#include "imgui.h"

#include "gl_hershey.h"
#include <gl\glfw.h>

Imgui* Imgui::_instance = 0x0;

Imgui::Imgui()
{
	_instance = this;
}

Imgui * Imgui::Instance()
{
	if( !_instance ){
		_instance = new Imgui();
	}

	return _instance;
}

bool Imgui::squareBtn(float x, float y, float w, float h, std::string txt)
{
	bool colliding = false;

	

	if( PointRectCollide( (float)Mouse.Mx, (float)Mouse.My, x, y, w, h) )
	{
		colliding = true;
		glColor4ub(200,0,0,128);
		fillRect(x, y, w, h);
	}
	else{
		//drawRect(x, y, w, h);
		glColor4ub(255,0,0,128);
		fillRect(x, y, w, h);
	}

	
	glColor3ub(255,255,255);
	HersheyText::Instance()->centerText( x + 15,y + h/2 );
	HersheyText::Instance()->glPrintf( txt.c_str() ); 

	if( Mouse.Mleft && colliding)
	{
		return true;
	}


	return false;
}

void Imgui::drawRect(float sx, float sy, float wx, float wy)
{
	glBegin(GL_LINES);
	glVertex2f(sx, sy);
	glVertex2f(sx+wx, sy+0);

	glVertex2f(sx, sy+wy);
	glVertex2f(sx+wx, sy+wy);

	glVertex2f(sx, sy);
	glVertex2f(sx, sy+wy);

	glVertex2f(sx+wx, sy);
	glVertex2f(sx+wx, sy+wy);

	glEnd();
}


void Imgui::fillRect( float sx, float sy, float wx, float wy)
{
	glBegin(GL_QUADS);
	glVertex2f(sx,sy+wy);
	glVertex2f(sx+wx,sy+wy);
	glVertex2f(sx+wx,sy);
	glVertex2f(sx,sy);
	glEnd();
}

bool Imgui::PointRectCollide(float x1, float y1, float x2, float y2, float w, float h)
{
	if(x1 >= x2 && x1 <= x2+w)
		if(y1 >= y2 && y1 <= y2+h)
			return true; 
	return false;
}

void Imgui::setMouseInfo( MouseInfo &mouseInfo )
{
	this->Mouse = mouseInfo.copy();
}
